﻿using OneOfAll.Common.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneOfAll.Common.Bussiness
{
    public class DataCenterManager
    {
        private static DataCenterManager _Instance = new DataCenterManager();
        public static DataCenterManager Instance
        {
            get { return _Instance; }
        }

        List<string> tempNameList = new List<string>();
        public ArchiveModel GetNewArchive()
        {
            ArchiveModel archive = new ArchiveModel();
            archive.CityInfo.AvailableConstructionList = new ObservableCollection<ConstructionModel>(Global.GlobalResource.ResourceConfig.ConstructionList.Where(t => t.IsInit));
            archive.MaterialsResource.Money = 1000;
            archive.MaterialsResource.Food = 500;
            archive.MaterialsResource.Wood = 300;
            archive.MaterialsResource.Stone = 100;
            tempNameList.Clear();
            for (int i = 0; i < 3; i++)
            {
                archive.PeopleList.Add(GetNewPeople(1, 4));
            }
            return archive;
        }

        public PeopleModel GetNewPeople(int minPotential, int maxPotential, int sex = -1)
        {
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            PeopleModel people = new PeopleModel();
            people.Sex = sex < 0 ? (SexEnum)rd.Next(2) : (SexEnum)sex;
            people.Potential_Power = rd.Next(minPotential, maxPotential);
            people.Potential_Trick = rd.Next(minPotential, maxPotential);
            people.Potential_Strong = rd.Next(minPotential, maxPotential);
            people.Potential_Wit = rd.Next(minPotential, maxPotential);
            people.Introduce = $"{Global.GlobalResource.AddrList[rd.Next(0, Global.GlobalResource.AddrList.Count)]}人士,为人{Global.GlobalResource.SpecialList[rd.Next(0, Global.GlobalResource.SpecialList.Count)]}";
            var list = people.Sex == SexEnum.男 ? Global.GlobalResource.ManFirstNameList : Global.GlobalResource.WomanFirstNameList;
            while (true)
            {
                var name = $"{Global.GlobalResource.LastNameList[rd.Next(0, Global.GlobalResource.LastNameList.Count)]}{list[rd.Next(0, list.Count)]}";
                if (Global.GlobalResource.ArchiveInfo == null)
                {
                    if (!tempNameList.Contains(name))
                    {
                        people.Name = name;
                        tempNameList.Add(name);
                        break;
                    }
                }
                else
                {
                    if (Global.GlobalResource.ArchiveInfo.PeopleList.FirstOrDefault(t => t.Name == name) == null)
                    {
                        people.Name = name;
                        break;
                    }
                }
            }
            people.EquipmentList.Add(new EquipmentModel() { Type = EquipmentTypeEnum.帽子, Name = "两耳生风" });
            people.EquipmentList.Add(new EquipmentModel() { Type = EquipmentTypeEnum.衣服, Name = "衣不蔽体" });
            people.EquipmentList.Add(new EquipmentModel() { Type = EquipmentTypeEnum.腰带, Name = "腰无长物" });
            people.EquipmentList.Add(new EquipmentModel() { Type = EquipmentTypeEnum.鞋子, Name = "赤足不履" });
            people.EquipmentList.Add(new EquipmentModel() { Type = EquipmentTypeEnum.饰品, Name = "空空如也" });
            people.EquipmentList.Add(new EquipmentModel() { Type = EquipmentTypeEnum.武器, Name = "手无寸铁" });
            return people;
        }

        public void WorkEffectCompleteHand(ConstructionModel construction, PeopleModel people)
        {
            switch (construction.ConstructionType)
            {
                case ConstructionTypeEnum.生产:
                    construction.ProductionInfo.CurrentEffect += people.GetProductEffect(construction.ProductionInfo.ProductionType);
                    people.Exp_Live += construction.Exp_Work;
                    break;
                case ConstructionTypeEnum.培养:
                    people.Exp_Power += construction.TrainInfo.Train_Power * people.Potential_Power;
                    people.Exp_Trick += construction.TrainInfo.Train_Trick * people.Potential_Trick;
                    people.Exp_Strong += construction.TrainInfo.Train_Strong * people.Potential_Strong;
                    people.Exp_Wit += construction.TrainInfo.Train_Wit * people.Potential_Wit;
                    break;
                case ConstructionTypeEnum.制作:
                    var product = Global.GlobalResource.ArchiveInfo.ConsumablesInfo.ConsumableList.FirstOrDefault(t => t.Type == construction.MakeInfo.ProductType);
                    if (product == null)
                    {
                        product = new ConsumablesModel() { Type = construction.MakeInfo.ProductType, Num = 0 };
                        Global.GlobalResource.ArchiveInfo.ConsumablesInfo.ConsumableList.Add(product);
                    }
                    product.Num += 1;
                    people.Exp_Live += construction.Exp_Work;
                    break;
                default:
                    break;
            }
            people.WorkKey = -1;
            people.Vitality -= 10;
            people.Vitality = people.Vitality > 0 ? people.Vitality : 0;
            people.Status = ObjectStatusEnum.空闲;
            people.X = construction.EntranceX * Global.GlobalResource.GridSize;
            people.Y = construction.EntranceY * Global.GlobalResource.GridSize;
        }

        public bool MaterialsResourceCheck(MaterialsResourceModel src, MaterialsResourceModel des, double radio)
        {
            return src.Money >= (des.Money * radio) && src.Food >= (des.Food * radio) && src.Wood >= (des.Wood * radio) &&
                        src.Stone >= (des.Stone * radio) && src.Iron >= (des.Iron * radio) && src.Copper >= (des.Copper * radio) &&
                        src.Cloth >= (des.Cloth * radio) && src.Silk >= (des.Silk * radio) && src.Gem >= (des.Gem * radio);
        }

        public void MaterialsResourceAdd(MaterialsResourceModel src, MaterialsResourceModel des, double radio)
        {
            src.Money += (int)(des.Money * radio);
            src.Food += (int)(des.Food * radio);
            src.Wood += (int)(des.Wood * radio);
            src.Stone += (int)(des.Stone * radio);
            src.Iron += (int)(des.Iron * radio);
            src.Copper += (int)(des.Copper * radio);
            src.Cloth += (int)(des.Cloth * radio);
            src.Silk += (int)(des.Silk * radio);
            src.Gem += (int)(des.Gem * radio);
        }

        public void MaterialsResourceRemove(MaterialsResourceModel src, MaterialsResourceModel des, double radio)
        {

            src.Money -= ((int)(des.Money * radio) > 0 ? (int)(des.Money * radio) : (des.Money > 0 ? 1 : 0));
            src.Food -= ((int)(des.Food * radio) > 0 ? (int)(des.Food * radio) : (des.Food > 0 ? 1 : 0));
            src.Wood -= ((int)(des.Wood * radio) > 0 ? (int)(des.Wood * radio) : (des.Wood > 0 ? 1 : 0));
            src.Stone -= ((int)(des.Stone * radio) > 0 ? (int)(des.Stone * radio) : (des.Stone > 0 ? 1 : 0));
            src.Iron -= ((int)(des.Iron * radio) > 0 ? (int)(des.Iron * radio) : (des.Iron > 0 ? 1 : 0));
            src.Copper -= ((int)(des.Copper * radio) > 0 ? (int)(des.Copper * radio) : (des.Copper > 0 ? 1 : 0));
            src.Cloth -= ((int)(des.Cloth * radio) > 0 ? (int)(des.Cloth * radio) : (des.Cloth > 0 ? 1 : 0));
            src.Silk -= ((int)(des.Silk * radio) > 0 ? (int)(des.Silk * radio) : (des.Silk > 0 ? 1 : 0));
            src.Gem -= ((int)(des.Gem * radio) > 0 ? (int)(des.Gem * radio) : (des.Gem > 0 ? 1 : 0));
        }
    }
}
